package com.ruoyi.project.mahjong.service.impl;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.mahjong.domain.TMemberLog;
import com.ruoyi.project.mahjong.domain.enu.MemberLogType;
import com.ruoyi.project.mahjong.domain.vo.MemberEventVo;
import com.ruoyi.project.mahjong.domain.vo.UpgradReportVo;
import com.ruoyi.project.mahjong.mapper.*;
import com.ruoyi.project.mahjong.service.ITMemberLogService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户操作记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-12
 */
@Service
public class TMemberLogServiceImpl implements ITMemberLogService {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private TMemberLogMapper tMemberLogMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private TOpenMoneyMapper moneyMapper;
    @Autowired
    private TOpenTableMapper openTableMapper;

    @Autowired
    private OtherPriceMapper otherPriceMapper;
    @Autowired
    private TWorkMapper tWorkMapper;

    /**
     * 查询用户操作记录
     *
     * @param id 用户操作记录ID
     * @return 用户操作记录
     */
    @Override
    public TMemberLog selectTMemberLogById(Long id) {
        return tMemberLogMapper.selectTMemberLogById(id);
    }

    /**
     * 查询用户操作记录列表
     *
     * @param tMemberLog 用户操作记录
     * @return 用户操作记录
     */
    @Override
    public List<TMemberLog> selectTMemberLogList(TMemberLog tMemberLog) {

        return tMemberLogMapper.selectTMemberLogList(tMemberLog);
    }

    /**
     * 新增用户操作记录
     *
     * @param tMemberLog 用户操作记录
     * @return 结果
     */
    @Override
    public int insertTMemberLog(TMemberLog tMemberLog) {
        tMemberLog.setCreateTime(DateUtils.getNowDate());
        return tMemberLogMapper.insertTMemberLog(tMemberLog);
    }

    /**
     * 修改用户操作记录
     *
     * @param tMemberLog 用户操作记录
     * @return 结果
     */
    @Override
    public int updateTMemberLog(TMemberLog tMemberLog) {
        tMemberLog.setUpdateTime(DateUtils.getNowDate());
        return tMemberLogMapper.updateTMemberLog(tMemberLog);
    }

    /**
     * 批量删除用户操作记录
     *
     * @param ids 需要删除的用户操作记录ID
     * @return 结果
     */
    @Override
    public int deleteTMemberLogByIds(Long[] ids) {
        return tMemberLogMapper.deleteTMemberLogByIds(ids);
    }

    /**
     * 删除用户操作记录信息
     *
     * @param id 用户操作记录ID
     * @return 结果
     */
    @Override
    public int deleteTMemberLogById(Long id) {
        return tMemberLogMapper.deleteTMemberLogById(id);
    }

    @Override
    public List<TMemberLog> selectTMemberLogByUserId(Long memberId) {
        return tMemberLogMapper.selectTMemberLogByUserId(memberId);
    }

    @Override
    public List<MemberEventVo> selectMemberEvent(TMemberLog tMemberLog) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        tMemberLog.setShopId(shopId);
        return tMemberLogMapper.selectMemberEvent(tMemberLog);
    }

    @Override
    public Integer updateTMemberLogBy0rderNumber(TMemberLog tMemberLog2) {
        return tMemberLogMapper.updateTMemberLogBy0rderNumber(tMemberLog2);
    }

    @Override
    public Integer updateOneOverTableTMemberLogBy0rderNumber(TMemberLog tMemberLog2) {
        return tMemberLogMapper.updateOneOverTableTMemberLogBy0rderNumber(tMemberLog2);
    }

    @Override
    public List<UpgradReportVo> selectReport(String startTime, String endTime) {

        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user.getUserId());
        List<SysUser> userList = userMapper.selectUserListByShopId(shopId);
        List<UpgradReportVo> reportVoList=tWorkMapper.selectReprot(startTime,endTime,shopId) ;
        for(SysUser sysUser:userList){
            boolean falg=true;
            for(UpgradReportVo vo:reportVoList){
                if(sysUser.getUserId().equals(vo.getStaffId())){
                    falg=false;
                }
            }
            if(falg){
                UpgradReportVo vo=new UpgradReportVo();
                vo.setCaseMoney(new BigDecimal(0));
                vo.setOtherMoney(new BigDecimal(0));
                vo.setOutMoney(new BigDecimal(0));
                vo.setSendMoney(new BigDecimal(0));
                vo.setScanMoney(new BigDecimal(0));
                vo.setRefendMoney(new BigDecimal(0));
                vo.setStaffId(sysUser.getUserId());
                vo.setStaffName(sysUser.getNickName());
                reportVoList.add(vo);
            }

        }
        for(UpgradReportVo vo:reportVoList){
            BigDecimal totalMoeny=vo.getCaseMoney().add(vo.getOtherMoney()).add(vo.getScanMoney()).subtract(vo.getOutMoney()).subtract(vo.getRefendMoney());
            vo.setTotalMoney(totalMoeny);
        }

        /*List<UpgradReportVo> oneList = tMemberLogMapper.selectReport(startTime, endTime, shopId);
        reportVoList = coverOneList(userList, oneList);//核算，充值收入，赠送收入，退款收入
        List<UpgradReportVo> twoList = moneyMapper.selectReport(startTime, endTime, shopId);
        coverTwoList(reportVoList,twoList);//饮料收入
        List<UpgradReportVo> threeList = openTableMapper.selectReport(startTime, endTime, shopId);
        coverThreeList(reportVoList,threeList);//桌台消费
        List<UpgradReportVo>   fouerList=otherPriceMapper.selectReport(startTime,endTime,shopId,null);
        coverFouerList(reportVoList,fouerList);//其他收入*/
        return reportVoList;
    }

    /**
     * 出来其他收入
     * @param reportVoList
     * @param fouerList
     */
    private void coverFouerList(List<UpgradReportVo> reportVoList, List<UpgradReportVo> fouerList) {
        for(UpgradReportVo reportVo:reportVoList){
            for(UpgradReportVo vo:fouerList){
                if(reportVo.getStaffId().equals(vo.getStaffId())){
                   // reportVo.setOtherCount(vo.getMoney());
                }
            }

        }
    }

    /**
     * 整合桌台消费
     * @param reportVoList
     * @param threeList
     */
    private void coverThreeList(List<UpgradReportVo> reportVoList, List<UpgradReportVo> threeList) {
        for(UpgradReportVo reportVo:reportVoList){
            for(UpgradReportVo vo:threeList){
                if(reportVo.getStaffId().equals(vo.getStaffId())){
                   // reportVo.setDeskCount(vo.getMoney());
                }
            }

        }
    }

    /**
     * 整合饮料消费
     * @param reportVoList
     * @param twoList
     */
    private void coverTwoList(List<UpgradReportVo> reportVoList, List<UpgradReportVo> twoList) {

        for(UpgradReportVo reportVo:reportVoList){
            for(UpgradReportVo vo:twoList){
                if(reportVo.getStaffId().equals(vo.getStaffId())){
                  //  reportVo.setCommidyCount(vo.getMoney());
                }
            }

        }

    }

    /**
     * 整合充值、退款、赠送金额
     * @param userList
     * @param oneList
     */
    private List<UpgradReportVo> coverOneList(List<SysUser> userList, List<UpgradReportVo> oneList) {
        List<UpgradReportVo> voList = new ArrayList<>();
        for (SysUser sysUser : userList) {

            UpgradReportVo reportVo=new UpgradReportVo();
            reportVo.setStaffName(sysUser.getNickName());
            reportVo.setStaffId(sysUser.getUserId());
            for (UpgradReportVo vo : oneList) {

                if (vo.getStaffId().equals(sysUser.getUserId())) {
                    if(vo.getEvent().equals(MemberLogType.CZ.getValue())){//充值
                       // reportVo.setAmountCount(vo.getMoney());
                    }else  if(vo.getEvent().equals(MemberLogType.QK.getValue())){//退款
                       // reportVo.setRefuCount(vo.getMoney());
                    }else if(vo.getEvent().equals(MemberLogType.ZS.getValue())){//赠送
                       /// reportVo.setSendCount(vo.getSendMoney());
                    }


                }
            }
            voList.add(reportVo);
        }
        return  voList;

    }


}
